<?php


namespace app\zmt\controller;

use api\app\model\GoodsModel;
use app\zmt\validate\NewsVaildate;
use cmf\controller\AdminBaseController;
use think\facade\Db;


class GoodsController extends AdminBaseController
{
    public function index(){
        $key = input('key');
        $where = [];
        if($key){
            $where[] = ['name', 'like', '%' . $key . '%'];
        }
        $list =GoodsModel::order('list_order', 'desc')
            ->where($where)
            ->paginate(3);
        return $this->fetch('index',[
            'list'=>$list
        ]);
    }

    public function add(){
        $list = GoodsModel::select();

        return $this->fetch('add',[
            'list'=>$list,
        ]);
    }
    public function addpost(){
        $data = input('post.');
        $data['images'] = implode(',',$data['images']);

        $validate = new NewsVaildate();
        $result = $validate->scene('add1')->check($data);
        if (!$result) {
            $this->error($validate->getError());
        }
        if (GoodsModel::create($data)) {
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }
    }
    public function edit(){
        $id = input('id');
        $list = GoodsModel::find($id);
        return $this->fetch('edit',['list' => $list]);
    }

    public function editPost(){

        $data = input('post.');
        $validate = new NewsVaildate();
        $result = $validate->scene('add1')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        $list = GoodsModel::find($data['id']);
        if(!$list){
            $this->error('未查询的记录');
        }
        if($list->update($data)){
            $this->success('保存成功','Goods/index');
        }else{
            $this->error('保存失败');
        }
    }

    public function del()
    {
        $id = input('id/d');
        $news = GoodsModel::destroy($id);
        if ($news) {
            $this->success('删除成功','Goods/index');
        } else {
            $this->success('删除失败');
        }
    }
    public function order()
    {
        parent::listOrders(new GoodsModel());
        $this->success('排序成功');
    }
    public function changeStatus()
    {
        $id = input('id/d');
        $list = GoodsModel::find($id);
        if ($list['status'] == 0) {
            $listStatus = 1;
        } else {
            $listStatus = 0;
        }

        $list->type = $listStatus;

        if ($list->save()) {
            $this->success('修改成功');
        } else {
            $this->success('修改失败');
        }
    }
    public function GoodsDetails(){
        $goodsId = input('goods_id');
        $this->assign('goods_id',$goodsId);
        return $this->fetch('detail');
    }
    //配置提交
    public function GoodsDetailsPost(){
        $attrs = input('post.from');
        $goodsId = input('post.goods_id');
        if(count($attrs)-1 < 1){
            $this->error('请至少配置一个属性！');
        }
        Db::name('goods_details')
            ->where('goods_id',$goodsId)
            ->delete();
        $insert_data = [];
        foreach($attrs as $attr){
            $insert_data[] = [
                'goods_id' => $goodsId,
                'name' => $attr['attr']
            ];
        }
        Db::name('goods_details')->insertAll($insert_data);
        $this->success('保存成功','Goods/index');
    }
    public function GoodsDetailvalue(){
        $goodsId = input('goods_id');
        $this->assign('goods_id',$goodsId);
        return $this->fetch('detail_value');
    }
    public function GoodsDetailattr(){

        $goodsId = input('goods_id');
        $list = Db::name('goods_details')
            ->where('goods_id',$goodsId)
            ->select();
        return json(['code'=>1, 'msg'=>'商品属性', 'data' => $list]);
    }
    public function GoodsDetailPost(){
        $data = input('post.from');

        $insert_data = [];
        foreach($data as $attr){
            $insert_data[] = [
                'name' => $attr['attr_value'],
                'g_id' => $attr['attr']
            ];
        }
        $insert = Db::name('goods_value')->insertAll($insert_data);
        if ($insert){
            return json(['code'=>1,'msg'=>'添加成功','data'=>"NULL"]);
        }else{
            return json(['code'=>0,'msg'=>'添加失败','data'=>"NULL"]);
        }
    }
    public function GoodsDetailattradd(){
        $goodsId = input('goods_id');
        $this->assign('goods_id',$goodsId);
        return $this->fetch('detail_attr');
    }
    public function GoodsDetailattrPost(){
        $attr = input('post.from');
        $goods_id = input('post.goods_id');
        $attr = json_encode($attr,JSON_UNESCAPED_UNICODE);
        $insert = GoodsModel::
        where('id','=',$goods_id)
            ->update(['attr'=>$attr]);

        if ($insert){
            return json(['code'=>1,'msg'=>'添加成功','data'=>"NULL"]);
        }else{
            return json(['code'=>0,'msg'=>'添加失败','data'=>"NULL"]);
        }
    }




}